package com.basic.modules.nykf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basic.common.utils.R;
import com.basic.modules.nykf.dao.ZzhDao;
import com.basic.modules.nykf.entity.CustomerShopBusinessEntity;
import com.basic.modules.nykf.entity.ZzhEntity;
import com.basic.modules.nykf.service.CustomerShopBusinessService;
import com.basic.modules.nykf.service.SubAccountService;
import com.basic.modules.nykf.service.ZzhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SubAccountServiceImpl implements SubAccountService {

    @Autowired
    ZzhService zzhService;

    @Autowired
    CustomerShopBusinessService customerShopBusinessService;

    @Autowired
    ZzhDao zzhDao;

    @Override
    public R operateZzhObj(ZzhEntity zzhEntity) {
        boolean flag;

        // 去除空格
        zzhEntity.setSjzh(zzhEntity.getSjzh().replaceAll("\\s", ""));
        zzhEntity.setZzhmc(zzhEntity.getZzhmc().replaceAll("\\s", ""));
        zzhEntity.setZzhmm(zzhEntity.getZzhmm().replaceAll("\\s", ""));

        // 根据店铺纬度校验子账号名称是否重复
        Long customerShopBusinessId = zzhEntity.getCustomerShopBusinessId();
        QueryWrapper<CustomerShopBusinessEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CustomerShopBusinessEntity::getCustomerShopBusinessId, customerShopBusinessId);
        CustomerShopBusinessEntity ywObj = customerShopBusinessService.getOne(queryWrapper);
        System.out.println(ywObj);

        Long customerShopId = ywObj.getCustomerShopId();
        Map map = new HashMap();
        map.put("customerShopId", customerShopId);
        map.put("zzhmc", zzhEntity.getZzhmc());
        if (zzhEntity.getZzhId() != null) {
            map.put("notZzhId", zzhEntity.getZzhId());
        }

        List<ZzhEntity> zzhList = zzhService.getZzhList(map);
        if (zzhList.size() > 0) {
            return R.error("添加子账号失败，该店铺已存在相同名称的子账号");
        }

        if (zzhEntity.getZzhId() != null) {
            flag = zzhService.updateById(zzhEntity);
        } else {
            zzhEntity.setZzhStatus(1);
            flag = zzhService.save(zzhEntity);
        }
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    public static void main(String[] args) {
        ZzhEntity zzhEntity = new ZzhEntity();
        zzhEntity.setSjzh(" 123 8 79 " +
                "s");
        System.out.println(zzhEntity.getSjzh().replaceAll("\\s", ""));
    }
}